<?php
/**
 * 
 * 
 * $Rev: 8 $
 * $Author: thejahweh $
 * $Date: 2010-09-14 09:40:11 +0200 (Di, 14 Sep 2010) $
 * $Id: cfg.class.php 8 2010-09-14 07:40:11Z thejahweh $
 * $URL: https://allone.googlecode.com/svn/trunk/plugins/core/cfg.class.php $
 * @author: Adrian Liechti <info@jahweh.ch>
 * @copyright: 2010 Adrian Liechti, GPL3 and CC3 BY-SA
 */

class cfg extends SimpleXMLElement {
	
	private static $mustSaved = false;
	private static $filePath = NULL;
	private static $invisible = NULL;
	private static $root = NULL;
	
	public function destruct () {
		if (self::$mustSaved == true) $this->save(true);
	}
	
	public function save ($now = false) {
		if ($now === false) {
			u::o('save -> mustSaved', 'cfg:save');
			return self::$mustSaved = true;
		} elseif (self::$filePath !== NULL) {
			$this->saveTo(self::$filePath, self::$invisible);
		} else {
			return false;
		}
	}
	
	public function saveTo ($savePath, $invisible = false) {
		u::o('saveTo: <span class="path">'.$savePath.'</span>', 'cfg:save');
		if (self::$root === NULL) return false;
		$dom_sxe = dom_import_simplexml(self::$root);
		if (!$dom_sxe) {
		    echo 'Error while converting XML';
		    exit;
		}
		
		$dom = new DOMDocument('1.0');
		$dom->formatOutput = true;
		$dom_sxe = $dom->importNode($dom_sxe, true);
		$dom_sxe = $dom->appendChild($dom_sxe);
		
		if ($invisible == true) {
			u::saveInvisibleFile($savePath, $dom->saveXML());
		} else {
			$dom->save($savePath);
		}
	}
	
	public function mustSaved () {
		return self::$mustSaved;
	}
	
	public function setRoot ($path, $invisible = true) {
		if (self::$filePath === NULL) {
			self::$filePath = $path;
		}
		if (self::$invisible === NULL) {
			self::$invisible = ($invisible === false ? false : true);
		}
		if (self::$root === NULL) {
			self::$root = $this;
		}
	}
	
}
?>